/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.form.core;

import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Form bean for string filter
 * </p>
 * <p><a href="StringFilterForm.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.10 $ $Date: 2006/08/03 10:08:06 $
 * @struts.form name="stringFilterForm"
 */
public class StringFilterForm extends BaseFilterForm {

	protected String firstCondition = FilterConditions.STRING_CONTAINS;
	protected String firstValue = "";
	protected String secondCondition = FilterConditions.CONDITION_NOT_SET;
	protected String secondValue = null;
	protected String logicCopula = FilterConditions.LOGIC_COPULA_AND;


	/**
	 * Creates the new instance of StringFilterForm
	 */
	public StringFilterForm() {
	}

    /**
     * Returns first condition
     *
     * @return first condition
     */
	public String getFirstCondition() {
		return firstCondition;
	}

    /**
     * Sets first condition
     *
     * @param firstCondition first condition to set
     */
	public void setFirstCondition(String firstCondition) {
		this.firstCondition = firstCondition;
	}

    /**
     * Returns value attached to first condition
     *
     * @return value attached to first condition
     */
	public String getFirstValue() {
		return firstValue;
	}

	/**
     * Sets value attached to first condition
     *
	 * @param firstValue value attached to first condition
	 */
	public void setFirstValue(String firstValue) {
		this.firstValue = firstValue;
	}

    /**
     * Returns second condition
     *
     * @return second condition
     */
	public String getSecondCondition() {
		return secondCondition;
	}

    /**
     * Sets second condition
     *
     * @param secondCondition second condition to set
     */
	public void setSecondCondition(String secondCondition) {
		this.secondCondition = secondCondition;
	}

    /**
     * Returns value attached to second condition
     *
     * @return value attached to second condition
     */
	public String getSecondValue() {
		return secondValue;
	}

	/**
     * Sets value attached to second condition
     *
	 * @param secondValue
	 */
	public void setSecondValue(String secondValue) {
		this.secondValue = secondValue;
	}

    /**
     * Returns logic copula between conditions
     *
     * @return logic copula
     */
	public String getLogicCopula() {
		return logicCopula;
	}

    /**
     * Sets logic copula between conditions
     *
     * @param logicCopula logic copula
     */
	public void setLogicCopula(String logicCopula) {
		this.logicCopula = logicCopula;
	}

	/**
	 * Resets all properties to their default values
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param request The non-http request we are proceeding
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);
		this.firstCondition = FilterConditions.STRING_CONTAINS;
		this.firstValue = "";
		this.secondCondition = FilterConditions.CONDITION_NOT_SET;
		this.secondValue = null;
		this.clearFilter = null;
		logicCopula = FilterConditions.LOGIC_COPULA_AND;
	}

	/**
	 * Form validation
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param request The non-http request we are proceeding
	 * @return Instance of ActionErrors contains all validation errors
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = super.validate(mapping, request);

		if ( clearFilter == null ) {

			boolean notSet = secondCondition.equalsIgnoreCase(FilterConditions.CONDITION_NOT_SET);
			boolean isNull = secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL);
			boolean isNotNull = secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL);

			if ( !notSet && !isNull && !isNotNull ) {
				if ( secondValue == null || secondValue.length() == 0 ) {
					errors.add("secondValue", new ActionMessage("core.grid.filter.errors.secondValueRequired"));
				}
			}
		} else {
			errors = new ActionErrors();
		}
		return errors;
	}


}